package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.util.ProgressDialogHelper;
import com.shundr.auth.common.view.CListView;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.truck.adapter.TruckAdapter;
import com.shundr.truck.http.UserTruckRequest;
import com.shundr.truck.model.TruckInfo;

public class UserTruckListActivity extends BaseActivity {

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 0;

	private List<TruckInfo> mList = new ArrayList<TruckInfo>();

	private TruckAdapter mAdapter;

	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
	private ImageView ivHope;

	private Button btnAdd;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case UserTruckRequest.GET_TRUCK_LIST_SUCCESSED:
					@SuppressWarnings("unchecked")
					List<TruckInfo> infos = (List<TruckInfo>) msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							if (pageNo == 0) {
								mList.clear();
								mListView.setVisibility(View.VISIBLE);
								mLayoutNoContent.setVisibility(View.GONE);
								if (infos.size() >= PAGE_SIZE) {
									mListView.setCanLoadMore(true);
									mListView.setAutoLoadMore(true);
									// mListView.setCanRefresh(true);
								} else {
									mListView.setCanLoadMore(false);
									mListView.setAutoLoadMore(false);
									// mListView.setCanRefresh(false);
								}
							}
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
							if (pageNo == 0) {
								mListView.setSelection(0);
							}
							pageNo = pageNo + 1;
						} else {
							if (pageNo == 0) {
								mListView.setVisibility(View.GONE);
								mLayoutNoContent.setVisibility(View.VISIBLE);
							} else {
								ToastHelper.toastLong(mContext, "最后一页");
							}
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_list);
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);
		tvNoContent = (TextView) findViewById(R.id.tv_no_content);
		ivHope = (ImageView) findViewById(R.id.iv_hope);
		ivHope.setImageResource(R.drawable.iv_no_truck);
		ivHope.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(mContext,
						AddTruckActivity.class), 888);
			}
		});
		btnAdd = (Button) findViewById(R.id.btn_add);
		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 添加页面
				startActivityForResult(new Intent(mContext,
						AddTruckActivity.class), 888);
			}
		});
		mLayoutNoContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 添加页面
				startActivityForResult(new Intent(mContext,
						AddTruckActivity.class), 888);
			}
		});
		// tvTitle.setText("我的车辆");
		tvNoContent.setText("您还未添加车辆,点我添加");
		mAdapter = new TruckAdapter(mContext, mList);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					Intent intent = new Intent(mContext,
							TruckDetailActivity.class);
					intent.putExtra("truckId", mList.get(position - 1)
							.getTruckId());
					intent.putExtra("id", mList.get(position - 1).getTruckId());
					startActivityForResult(intent, 888);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

		mListView.setOnRefreshListener(new CListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				new UserTruckRequest(mContext, mHandler).getTruckList(-1,
						pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new CListView.OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new UserTruckRequest(mContext, mHandler).getTruckList(-1,
						pageNo, PAGE_SIZE);
			}
		});
		
		btnRight.setVisibility(View.VISIBLE);
		btnRight.setText("添加");
		btnRight.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 添加页面
				startActivityForResult(
						new Intent(mContext, AddTruckActivity.class), 888);
				
			}
		});
		
		ProgressDialogHelper.show(mContext, "正在加载中...");
		new UserTruckRequest(mContext, mHandler).getTruckList(-1, pageNo,
				PAGE_SIZE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (resultCode) {
		case 888:
			pageNo = 0;
			new UserTruckRequest(mContext, mHandler).getTruckList(-1, pageNo,
					PAGE_SIZE);
			break;

		default:
			break;
		}
	}

}
